package com.kyzh.core.pager.weal.cloud

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.CloudGameBean
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.requests.GameRequest
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.activities.BrowserActivity
import com.kyzh.core.databinding.ActCloudgameBinding
import com.kyzh.core.databinding.ItemCloudTextviewBinding
import com.kyzh.core.databinding.ItemCloudgameBinding
import com.kyzh.core.utils.setVisibility
import com.kyzh.core.utils.startActivity
import com.kyzh.core.utils.startGameDetailActivity
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder
import kotlinx.android.synthetic.main.activity_myassets1.*
import org.jetbrains.anko.textColor

class CloudGameActivity : BaseActivity() {
    companion object{
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, CloudGameActivity::class.java)
            context.startActivity(starter)
        }
    }
    private lateinit var db :ActCloudgameBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db = DataBindingUtil.setContentView(this, R.layout.act_cloudgame)
        initData()
    }

    private fun initData() {
        GameRequest.getCloudGame{
            db.banner.setLifecycleRegistry(lifecycle)
                .setAdapter(Adapter())
                .create(this)
        }
    }


    private inner class Adapter :BaseBannerAdapter<CloudGameBean>(){
        override fun bindData(
            holder: BaseViewHolder<CloudGameBean>,
            data: CloudGameBean,
            position: Int,
            pageSize: Int
        ) {
           val item: ItemCloudgameBinding? = DataBindingUtil.bind(holder.itemView)
            item?.apply {

                linPaly.setOnClickListener {
                    GameRequest.getCloudGameState(data?.id){
                        startActivity<BrowserActivity>(GlobalKeys.TITLE to data?.name, GlobalKeys.LINK to data?.url)
                    }

                }
                linDown.setOnClickListener {
                    startGameDetailActivity(data?.gid)
                }
                linDown.setVisibility(!data?.gid.isNullOrBlank())
                item.data = data
                linTips.setVisibility(!data.tishi.isNullOrBlank())
                linPaly.isEnabled = data.style==1
                if( data.style == 0 ){
                    tvPlay.text = "已占用"
                }else{
                    tvPlay.text = "云玩"
                }
                revBq.layoutManager = LinearLayoutManager(this@CloudGameActivity,RecyclerView.HORIZONTAL,false)
                revBq.adapter = object : BaseQuickAdapter<String, BaseDataBindingHolder<ItemCloudTextviewBinding>>(R.layout.item_cloud_textview, data?.biaoqian) {

                    override fun convert(
                        holder: BaseDataBindingHolder<ItemCloudTextviewBinding>,
                        item: String
                    ) {
                        holder.dataBinding?.tvTags?.apply {
                            text = item
                            when(holder.layoutPosition){
                                0 ->{
                                    background = context.resources.getDrawable(R.drawable.bq9)
                                    textColor = context.resources.getColor(R.color.bq9)
                                }
                                1 ->{
                                    background = context.resources.getDrawable(R.drawable.bq8)
                                    textColor = context.resources.getColor(R.color.bq8)
                                }
                                2 ->{
                                    background = context.resources.getDrawable(R.drawable.bq6)
                                    textColor = context.resources.getColor(R.color.bq6)
                                }
                                else ->{
                                    background = context.resources.getDrawable(R.drawable.bq6)
                                    textColor = context.resources.getColor(R.color.bq6)
                                }
                            }
                        }
                    }
                }
            }
        }

        override fun getLayoutId(viewType: Int) = R.layout.item_cloudgame

    }
}